package org.erepublik.company.charts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.erepublik.company.EAlliance;
import org.erepublik.company.EProductType;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;
import org.erepublik.company.comparators.ProductComparatorByPriceInGold;
import org.erepublik.company.product.Weapons;
import org.erepublik.company.searchCriteria.EWeaponChartCriteria;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONWeaponService;
import org.erepublik.company.service.XmlVatService;
import org.json.JSONArray;
import org.json.JSONException;

public class WeaponJSONDataCollector extends AbstractJSONDataCollector<Weapons> {

    public WeaponJSONDataCollector(String productName, IVatService vatService) {
        super(productName, EWeaponChartCriteria.class, vatService);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected double calculateOffers(Weapons[] bestOffers, Enum criteria) {
        double attributeSum = 0.0d;
        double valueSum = 0.0d;
        for (Weapons food : bestOffers) {
            valueSum += food.getPriceInGold() * food.getQuantity();
            attributeSum += food.getQuantity();
            if (attributeSum > waponLimit)
                break;
        }
        if (valueSum == 0)
            return 0.0d;

        return valueSum / attributeSum;
    }

    private final int waponLimit = 10;

    
    
    
    @Override
    @SuppressWarnings("unchecked")
    protected Weapons[] getNBestOffers(int offers, ArrayList<Weapons> object, Enum criteria, Location country) {
        Weapons[] foodArray = filter(object, country).toArray(new Weapons[0]);

        Arrays.sort(foodArray, new ProductComparatorByPriceInGold());
        Weapons[] shorArray = new Weapons[Math.min(offers, foodArray.length)];
        for (int i = 0; i < shorArray.length; i++) {
            shorArray[i] = foodArray[i];
        }
        return shorArray;
    }

    
    
    
    
    private ArrayList<Weapons> filter(ArrayList<Weapons> object, Location country) {
        ArrayList<Weapons> newWeapons = new ArrayList<Weapons>();
        
        if (country instanceof EAlliance) {
            for (Weapons weapon : object) {
                if (weapon.getProductType() != getWeaponType(productName))
                    continue;
                if (weapon.getQuality() != 5)
                    continue;
                if (weapon.getDamage() < 10.0d)
                    continue;
                if (weapon.getCountry().getAlliance() != country)
                    continue;
                newWeapons.add(weapon);
            }
            return newWeapons;
        }
        
        
        for (Weapons weapon : object) {
            if (weapon.getProductType() != getWeaponType(productName))
                continue;
            if (weapon.getQuality() != 5)
                continue;
            if (country!=EWorld.GLOBAL && weapon.getCountry() != country)
                continue;
            newWeapons.add(weapon);
        }

        return newWeapons;
    }

    @Override
    protected ArrayList<Weapons> getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        return JSONWeaponService.getWeaponsFromJSONArray(jsonArrayWithFood, getWeaponType(productName));
    }

    private EProductType getWeaponType(String productName) {
        if (productName.equals("helicopter"))
            return EProductType.HELICOPTER;
        if (productName.equals("tank"))
            return EProductType.TANK;
        if (productName.equals("rifle"))
            return EProductType.RIFLE;
        if (productName.equals("artillery"))
            return EProductType.ARTILLERY;

        return null;
    }
    
    
    
    
    
    public static void main(String[] args) {
        WeaponJSONDataCollector dataCollector = new WeaponJSONDataCollector("helicopter", new XmlVatService());
        dataCollector.updateData();
        ChartDataSet dataset = dataCollector.getData(EAlliance.PHOENIX, EWeaponChartCriteria.Q5FULLDMG.toString());
        List<ChartData> data = dataset.getData();
        for (ChartData chartData : data) {
            System.out.println( new Date(chartData.getTime()) + " "+chartData.getValue());
            
        }
    }
    
    

}
